/**
 * Created by zhangwenlei on 2017/5/9.
 */
// (function(){
ADF.policy.ButtonGroupPolicy = function(){
    ADF.policy.ButtonGroupPolicy.superclass.constructor.call(this);
};

COM.util.defineClass(ADF.policy.ButtonGroupPolicy, COM.gef.XYLayoutEditPolicy, {
    showLayoutTargetFeedback:function (request) {
        var RequestConstants = COM.gef.RequestConstants;
        if (RequestConstants.REQ_CREATE == (request.getType())){
            var model = request.getNewObject();
            if(model instanceof ADF.model.ButtonModel) {
                var bounds = this.getHost().getFigure().getWidget().showFeedback(model.getLayoutData().height,model.getLayoutData().width);
                model.getLayoutData().x = bounds.x;
                model.getLayoutData().y = bounds.y;
                model.getLayoutData().height = bounds.height;
                model.getLayoutData().width=bounds.width;
            }
        }
    },
    eraseLayoutTargetFeedback: function(request) {
        this.getHost().getFigure().getWidget().eraseFeedback();
    },
    createChangeConstraintCommand: function(request, child, rect){
        if(request.getResizeDirection() != COM.draw2d.PositionConstants.EAST) {
            return COM.gef.UnexecutableCommand.INSTANCE;
        }
        if(rect.height > this.getHost().getFigure().getSize().height) {
            return COM.gef.UnexecutableCommand.INSTANCE;
        }
        if(rect.width > this.getHost().getFigure().getSize().width) {
            return COM.gef.UnexecutableCommand.INSTANCE;
        }

        var model = child.getModel();
        return COM.gef.Command.createCommand({
            label: 'change bounds',
            execute: function(){
                this.old = model.getLayoutData();
                model.setLayoutData(rect);
            },
            undo: function(){
                model.setLayoutData(this.old);
            }
        });

    },
    getCreateCommand: function(request){
        if(!(request.getNewObject() instanceof ADF.model.ButtonModel)) {
            return COM.gef.UnexecutableCommand.INSTANCE;
        }
        return COM.gef.Command.createCommand({
            label: 'create child',
            child: request.getNewObject(),
            parent: this.getHost().getModel(),
            figure: this.getHost().getFigure(),
            execute: function(){
                this.parent.addChild(this.child);
                this.child.setLayoutData(new COM.draw2d.Point(63,33));
            },
            undo: function(){
                this.parent.removeChild(this.child);
            }
        });
    },
    createAddCommand: function(request, child, rect) {
        if(!(child.getModel() instanceof ADF.model.ButtonForGroupModel)) {
            return COM.gef.UnexecutableCommand.INSTANCE;
        }
        var command = new COM.gef.CompoundCommand(), deleteCommand = null, addCommand = null;
        deleteCommand = COM.gef.Command.createCommand({
            label: 'delete model',
            parentModel: child.getParent().getModel(),
            childModel:child.getModel(),
            oldBounds: child.getModel().getLayoutData(),
            execute: function(){
                this.parentModel.removeChild(this.childModel);
            },
            undo: function(){
                this.parentModel.addChild(this.childModel);
                this.childModel.setLayoutData(this.oldBounds);
            }
        });
        addCommand = COM.gef.Command.createCommand({
            label: 'add model',
            parentModel: this.getHost().getModel(),
            childModel:child.getModel(),
            execute: function(){
                this.parentModel.addChild(this.childModel);
                this.childModel.setLayoutData(rect);
            },
            undo: function(){
                this.parentModel.removeChild(this.childModel);
            }
        });
        command.add(deleteCommand);
        command.add(addCommand);
        return command.unwrap();
    },
    getConstraintFor: function(request, child) {
        var rectangle = new COM.draw2d.Rectangle(),
            childBoundsCopy = child.getModel().getBounds().copy(),
            hostModel = this.getHost().getModel(),
            copy = hostModel.getBounds().copy(),
            hostFigure = this.getHost().getFigure(),
            childFigure = child.getFigure(),
            moveData = request.getMoveDelta();
        hostFigure.translateToAbsolute(copy);
        childFigure.translateToAbsolute(childBoundsCopy);
        var relativeLoaction = new COM.draw2d.Point(childBoundsCopy.x + moveData.x - copy.x  , childBoundsCopy.y + moveData.y - copy.y);
        rectangle.setLocation(relativeLoaction);
        rectangle.setSize(child.getFigure().getLayoutData().getSize());
        return rectangle;
    }
});
